package edu.gatech.geochat;

import edu.gatech.geochat.database.Storage;
import edu.gatech.geochat.model.User;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class RegisterActivity extends BetterActivity {

	private TextView userView, passView, emailView, cpassView;
	public static void launch(Activity source, String username, String password) {
		GUISpace.put(RegisterActivity.class, "username", username);
		GUISpace.put(RegisterActivity.class, "password", password);
		Intent n = new Intent(source, RegisterActivity.class);
		source.startActivity(n);
	}
	
	public void onCreate(Bundle b) {
		super.onCreate(b);
		setContentView(R.layout.register);
		
		userView  = (TextView) findViewById(R.id.registerUser);
		emailView = (TextView) findViewById(R.id.registerEmail);
		passView  = (TextView) findViewById(R.id.registerPass);
		cpassView = (TextView) findViewById(R.id.registerPassConfirm);
		
		setStatusText("");
	}
	
	@Override
	public void onPause() {
		super.onPause();
		GUISpace.put(getClass(), "username", userView.getText());
		GUISpace.put(getClass(), "email", emailView.getText());
		GUISpace.put(getClass(), "password", passView.getText());
		GUISpace.put(getClass(), "cpass", cpassView.getText());
	}
	
	@Override
	public void onResume() {
		super.onResume();
		userView.setText(GUISpace.getString(getClass(),  "username"));
		emailView.setText(GUISpace.getString(getClass(), "email"));
		passView.setText(GUISpace.getString(getClass(),  "password"));
		cpassView.setText(GUISpace.getString(getClass(), "cpass"));
	}

	private volatile boolean attemptingRegister = false;
	private synchronized void tryRegister(final String name, final String pass, final String email) {
		if (attemptingRegister) return;
		attemptingRegister = true;

		new Thread() {
			public void run() {
				String results = Storage.getInstance().addUser(name, pass, email);
				setStatusText(results);
				
				if (results == Storage.RESPONSE_SUCCESS) {
					User user = Storage.getInstance().getUser(name, pass);
					if (user != null)
						TitleActivity.launch(RegisterActivity.this, user);
					else {
						setStatusText("There may have been a connection error. Please try again.");
					}
				}
				attemptingRegister = false;
			}
		}.start();
	}
	
	public void register(View view) {
		if (attemptingRegister) return;
		
		String uname = userView.getText().toString().trim();
		String email = emailView.getText().toString().trim();
		String pass  = passView.getText().toString().trim();
		String cpass = cpassView.getText().toString().trim();
		
		if (uname.length() == 0) {
			setStatusText("Please choose a username.");
			return;
		}
		if (email.length() == 0) {
			setStatusText("Please enter your email address.");
			return;
		}
		if (pass.length() == 0) {
			setStatusText("Please choose a password.");
			return;
		}
		if (cpass.length() == 0) {
			setStatusText("Please confirm your password.");
			return;
		}
		
		if (!pass.equals(cpass)) {
			setStatusText("Passwords do not match.");
			return;
		}
		
		setStatusText("Sending data to server...");
		tryRegister(uname, pass, email);
	}
}
